<!-- views/components/element/FrameElement/index.vue -->

<template>
  <div
    class="editable-element-frame"
    :style="{
      top: elementInfo.top + 'px',
      left: elementInfo.left + 'px',
      width: elementInfo.width + 'px',
      height: elementInfo.height + 'px',
      backgroundColor: elementInfo.backgroundcolor.backgroundcolor,
      textAlign: elementInfo.center,
      lineHeight: elementInfo.height + 'px',
    }"
  >
  <div
        class="element-content"
        v-contextmenu="contextmenus"
        @mousedown="($event) => handleSelectElement($event)"
        @touchstart="($event) => handleSelectElement($event)"
      >
        <div>
          <div class="box">
            <img :src="elementInfo.urls.urls"  ref="myImage" v-once />
          </div>
        </div>

        <!-- <div class="drag-handler top"></div>
        <div class="drag-handler bottom"></div>
        <div class="drag-handler left"></div>
        <div class="drag-handler right"></div> -->

        <!-- <div
          class="mask"
          v-if="handleElementId !== elementInfo.id"
          @mousedown="($event) => handleSelectElement($event, false)"
          @touchstart="($event) => handleSelectElement($event, false)"
        ></div> -->
      </div>
    <!-- <div
      class="rotate-wrapper"
      :style="{ transform: `rotate(${elementInfo.rotate}deg)` }"
    >
      
    </div> -->
  </div>
</template>

<script lang="ts" setup>
import { PropType,onMounted,ref } from "vue";
import { storeToRefs } from "pinia";
import { useMainStore } from "@/store";
import { PPTHdminElement } from "@/types/slides";
import { ContextmenuItem } from "@/components/Contextmenu/types";

const props = defineProps({
  elementInfo: {
    type: Object as PropType<PPTHdminElement>,
    required: true,
  },
  selectElement: {
    type: Function as PropType<
      (
        e: MouseEvent | TouchEvent,
        element: PPTHdminElement,
        canMove?: boolean
      ) => void
    >,
    required: true,
  },
  contextmenus: {
    type: Function as PropType<() => ContextmenuItem[] | null>,
  },
});
const myImage = ref(null);

const disableMouseEvents = () => {
  if (myImage.value) {
    myImage.value.style.pointerEvents = 'none';
  }
};
const { handleElementId } = storeToRefs(useMainStore());

const handleSelectElement = (e: MouseEvent | TouchEvent, canMove = true) => {
  e.stopPropagation();
  props.selectElement(e, props.elementInfo, canMove);
};
onMounted(() => {
      disableMouseEvents();
    });
</script>

<style lang="scss" scoped>
.editable-element-frame {
  position: absolute;
}
.element-content {
  width: 100%;
  height: 100%;
  cursor: move;
}

.drag-handler {
  position: absolute;

  &.top {
    height: 20px;
    left: 0;
    right: 0;
    top: 0;
  }
  &.bottom {
    height: 20px;
    left: 0;
    right: 0;
    bottom: 0;
  }
  &.left {
    width: 20px;
    top: 0;
    bottom: 0;
    left: 0;
  }
  &.right {
    width: 20px;
    top: 0;
    bottom: 0;
    right: 0;
  }
}
.mask {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}
</style>
